package class5;

import common.TreeNode;
import common.TreeNodeUtils;

/**
 * https://leetcode.cn/problems/er-cha-shu-de-jing-xiang-lcof/description/
 * 反转二叉树
 * 思路：
 * 1.先将两个子节点翻转，再递归调用翻转方法
 */
public class Code11_flipTree {
    public static void main(String[] args) {
        TreeNode treeNode = TreeNodeUtils.getTreeNode();
        TreeNodeUtils.printTree(treeNode);
        TreeNode treeNode1 = flipTree(treeNode);
        TreeNodeUtils.printTree(treeNode1);
    }

    public static TreeNode flipTree(TreeNode treeNode) {
        if (treeNode == null) {
            return null;
        }
        // 先将两个子节点反转
        TreeNode tmp = treeNode.left;
        treeNode.left = treeNode.right;
        treeNode.right = tmp;

        flipTree(treeNode.left);
        flipTree(treeNode.right);

        return treeNode;
    }
}
